<script setup>
import { onMounted, ref } from "vue";
import HomePanel from "./HomePanel.vue";
import { getClickApi, getNewAPI } from "@/apis/home";

const newList = ref([]);
const getNew = async (num = 30) => {
  let res = await getNewAPI({ num });
  newList.value = res.products;
  console.log("newList", newList.value);
};

const getClick = async (id) => {
  console.log("点击了", id);
  await getClickApi(id);
};

onMounted(() => {
  getNew();
});
</script>

<template>
  <HomePanel title="实时推荐" subTitle="实时推荐 时尚精品">
    <!-- 下面是插槽主体内容模版 -->
    <ul class="goods-list">
      <li
        v-for="item in newList"
        :key="item.productId"
        @click.stop="getClick(item.productId)"
      >
        <RouterLink :to="`/detail/${item.productId}`">
          <img :src="item.imageUrl" alt="" />
          <p class="name">{{ item.name }}</p>
          <p class="price">{{ item.tags }}</p>
        </RouterLink>
      </li>
    </ul>
  </HomePanel>
</template>

<style scoped lang="scss">
.goods-list {
  display: grid;
  grid-template-columns: repeat(5, 1fr); // 固定 5 列
  gap: 30px; // 商品之间的间距

  list-style: none;

  li {
    width: 100%;
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden;
    padding: 1em;
    box-shadow: 6px 6px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease, box-shadow 0.3s ease;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
    }

    a {
      display: block;
      text-decoration: none;
      color: inherit;
    }

    img {
      width: 100%;
      height: auto;
      aspect-ratio: 1 / 1; // 保持正方形比例
      object-fit: cover;
    }

    .name {
      font-size: 16px;
      font-weight: 600;
      color: #333;
      padding: 10px 0 5px;
      text-align: center;

      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      max-width: 12rem;
    }

    .desc {
      font-size: 14px;
      color: #999;
      text-align: center;
      padding-bottom: 10px;
    }
  }

  // 响应式：屏幕较小时减少列数
  @media (max-width: 1200px) {
    grid-template-columns: repeat(4, 1fr);
  }

  @media (max-width: 992px) {
    grid-template-columns: repeat(3, 1fr);
  }

  @media (max-width: 768px) {
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
  }

  @media (max-width: 480px) {
    grid-template-columns: 1fr;
  }
}
</style>
